package com.pateo.bjev.contacts.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.pateo.bjev.contacts.R;
import com.pateo.bjev.contacts.utils.CallUtils;
import com.pateo.bjev.contacts.utils.Log;
import com.pateo.bluetooth.logic.bean.ContactLog;
import com.pateo.bluetooth.logic.util.Constants;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by pateo on 17-5-9.
 */

public class CallLogAdapter extends RecyclerView.Adapter<CallLogAdapter.ViewHolder> {

    private Context context;
    private List<ContactLog> logList = new LinkedList<>();

    public CallLogAdapter(Context context ,List<ContactLog> logList){
        this.context = context;
        if(logList!=null){
            this.logList = logList;
        }
    }

    public void updateList(List<ContactLog> logs){
        if(logs!=null){
            logList = logs;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.calllog_item_layout,null);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ContactLog log = logList.get(position);

        holder.call_name.setText(log.getName());
        holder.call_num.setText(log.getTelNum());
//        holder.call_duration.setText(log.get);

        if(log.getTimeList()!=null && log.getTimeList().size()>0){
            holder.call_time.setText(CallUtils.formatTime(log.getTimeList().get(0)));
        }

//        for(String s : log.getTimeList()){
//            Log.d(CallLogAdapter.class,"name:"+log.getName()+",time:"+s);
//        }

        int type = log.getType();
        if(type == Constants.CALLLOG_TYPE_ALL){
            holder.call_icon.setImageResource(android.R.drawable.sym_action_call);
        }else if(type == Constants.CALLLOG_TYPE_INCOMING){
            holder.call_icon.setImageResource(android.R.drawable.sym_call_incoming);
        }else if(type == Constants.CALLLOG_TYPE_MISSED){
            holder.call_icon.setImageResource(android.R.drawable.sym_call_missed);
        }else if(type == Constants.CALLLOG_TYPE_OUTGOING){
            holder.call_icon.setImageResource(android.R.drawable.sym_call_outgoing);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CallUtils.call(context,log.getTelNum());
            }
        });
    }

    @Override
    public int getItemCount() {
        return logList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView call_icon;
        public TextView call_name,call_num,call_time;

        public ViewHolder(View itemView) {
            super(itemView);

            call_icon = (ImageView) itemView.findViewById(R.id.call_icon);
            call_name = (TextView) itemView.findViewById(R.id.call_name);
            call_num = (TextView) itemView.findViewById(R.id.call_num);
            call_time = (TextView) itemView.findViewById(R.id.call_time);
        }
    }

}
